package com.yang.controller;

import com.yang.entity.NoteThings;
import com.yang.entity.UserThings;
import com.yang.service.NoteThingsService;
import com.yang.service.UserThingsService;
import com.yang.utils.RedisFactoryUtils;
import com.yang.utils.UtilsFactory;
import com.yang.entity.EmailSmsCountInfo;
import com.yang.utils.info.NoteResponseInfo;
import com.yang.entity.ProfileThings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 用户事件表(UserThings)表控制层
 *
 * @author yangdacheng
 * @since 2022-03-02 19:11:03
 */
@RestController
@RequestMapping("userThings")
@Slf4j
public class UserThingsController {
    /**
     * 服务对象
     */
    @Resource
    private UserThingsService userthingsService;
    @Resource
    private NoteThingsService noteThingsService;
    @Resource
    private UtilsFactory utilsFactory;
    @Resource
    private RedisFactoryUtils redisFactoryUtils;

    /**
     * 根据用户编号获取用户事件
     * @param uid
     * @return
     */
    @GetMapping("queryUserThings/{uid}")
    public NoteResponseInfo queryAllThings(@PathVariable("uid") String uid){
        log.info("userID------->"+uid);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        List<UserThings> userThings = userthingsService.queryThingsByUID(uid);
        List<NoteThings> noteThings = new ArrayList<>();
        Iterator<UserThings> iterator = userThings.iterator();
        while (iterator.hasNext()){
            noteThings.add(iterator.next().getNoteThings());
        }
        if (userThings.size()>0){
            noteResponseInfo.setNoteCode(200).setNoteMessage("获取事件成功").setNoteClass(noteThings);
        }else{
            noteResponseInfo.setNoteClass(300).setNoteClass("当前还没有日程信息哟").setNoteClass(null);
        }
        return noteResponseInfo;
    }

    /**
     * 添加用户事件
     * @param noteThings
     * @param uid 用户编号
     * @param start 开始时间（str）
     * @param end 结束事件
     * @return
     */
    @PostMapping("insertThings")
    public NoteResponseInfo addThings(NoteThings noteThings,String uid,String start,String end){
        log.info("传递将来的事件："+noteThings);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        noteThings.setStartTime(start)
                .setEndTime(end)
                .setThingsId(utilsFactory.buildID()).setIsSend(0); //设置事件对象
        UserThings userThings = new UserThings();
        userThings.setUserThingsId(utilsFactory.buildID())
                .setThingsId(noteThings.getThingsId())
                .setUserid(uid);//设置用户事件对象
        log.info("事件--》"+noteThings);
        //判断是否在缓存中 不在添加到缓存
        redisFactoryUtils.insetToList("thingsTypes",noteThings.getThingsType());
        Boolean insThings = noteThingsService.insertNoteThings(noteThings); //添加事件
        if (insThings){
            //添加事件成功 -- 添加用户事件
            Boolean insertUserThings = userthingsService.insertUserThings(userThings);
            if (insertUserThings){
                noteResponseInfo.setNoteCode(200).setNoteMessage("添加成功").setNoteClass(userThings);
            }else{
                noteResponseInfo.setNoteCode(281).setNoteMessage("添加用户事件错误").setNoteClass(null);
            }
        }else{
            noteResponseInfo.setNoteCode(291).setNoteMessage("添加事件错误").setNoteClass(null);
        }
        return noteResponseInfo;
    }

    /**
     * 根据用户编号删除事件
     * @param noteThingsID
     * @return
     */
    @DeleteMapping("deleteThings/{noteId}")
    public NoteResponseInfo deleteThings(@PathVariable("noteId") String noteThingsID){
        log.info("noteThingsID:"+noteThingsID);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        Boolean deleteUserThings = userthingsService.deleteUserThings(noteThingsID);
        if (deleteUserThings){
            Boolean deleteThings = noteThingsService.deleteNoteThings(noteThingsID);
            if(deleteThings){
                noteResponseInfo.setNoteCode(200).setNoteMessage("删除成功");
            }else{
                noteResponseInfo.setNoteCode(292).setNoteMessage("删除事件失败");
            }
        }else{
            noteResponseInfo.setNoteCode(293).setNoteMessage("删除用户事件失败");
        }
        return noteResponseInfo;
    }

    /**
     * 获取用户未打卡数量
     * @param uid
     * @return
     */
    @GetMapping("getNoPunch/{uid}")
    public NoteResponseInfo getNoPunch(@PathVariable("uid") String uid){
        String profileTitle = "待做清单数量";
        String background = "#FFF9C4";
        Integer profileNumber = 0;
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        ProfileThings profileThings = new ProfileThings();
        List<UserThings> userThings = userthingsService.queryThingsByUID(uid); //获取用户所有事件
        Iterator<UserThings> iterator = userThings.iterator();
        while (iterator.hasNext()){
            Integer isPunch = noteThingsService.queryPunchByState(iterator.next().getThingsId());
            if (isPunch == 0){
                profileNumber++;
            }
        }
        profileThings.setProfileTitle(profileTitle).setProfileNumber(profileNumber).setBackground(background);
        noteResponseInfo.setNoteCode(200).setNoteMessage("获取成功").setNoteClass(profileThings);
        return noteResponseInfo;
    }

    /**
     * 获取图3的数据
     * @param uid
     * @return
     */
    @GetMapping("getPictureThree/{uid}")
    public NoteResponseInfo getPictureThree(@PathVariable("uid")String uid){
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        List<UserThings> userThings = userthingsService.queryThingsByUID(uid);  //获取用户所有时间
        Iterator<UserThings> iterator = userThings.iterator();
        Integer emailCount = 0; //邮箱计数
        Integer smsCount = 0; //短信计数
        while (iterator.hasNext()){
            NoteThings noteThings = noteThingsService.queryThingsByID(iterator.next().getThingsId());
            if (noteThings.getIsPunchCard()== 0 && noteThings.getIsEmailStart() == 1){
                emailCount++;
            }
            if (noteThings.getIsPunchCard()== 0 && noteThings.getIsTelStart() == 1){
                smsCount++;
            }
        }
        EmailSmsCountInfo countInfo = new EmailSmsCountInfo("",emailCount,smsCount);
        noteResponseInfo.setNoteCode(200).setNoteClass(countInfo);
        return noteResponseInfo;
    }
}